package EXam.Question16;

import java.util.Objects;

public class Person {
    private String name;
    private int age;
    private String address;
    
    // 空参构造方法
    public Person() {
    }
    
    // 带参构造方法
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    // getter和setter方法
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    // 重写toString方法
    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + ", address='" + address + "'}";
    }
    
    // 重写equals和hashCode方法，用于去重
    @Override
    public boolean equals(Object o) {
        // 如果当前对象和传入的对象是同一个对象，则返回true
        if (this == o) return true;
        // 如果传入的对象为null或者当前对象的类和传入对象的类不同，则返回false
        if (o == null || getClass() != o.getClass()) return false;
        // 将传入的对象转换为Person类型
        Person person = (Person) o;
        // 比较当前对象的age属性和传入对象的age属性是否相等
        return age == person.age &&
               // 比较当前对象的name属性和传入对象的name属性是否相等
               Objects.equals(name, person.name) &&
               // 比较当前对象的address属性和传入对象的address属性是否相等
               Objects.equals(address, person.address);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(name, age, address);
    }
}